﻿using System;

namespace HW3_c1
{
    //链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)
        {
            Next = null;
            Data = t;
        }

    }
    //泛型链表类
    public class GenericList<T> 
    {
        private Node<T> head;
        private Node<T> tail;
        public GenericList()
        {
            tail = head = null;
        }
        public Node<T> Head
        {
            get => head;
        }
        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if(tail == null)
            {
                head = tail = n;
            }
            else
            {
                // n.Next = tail.Next; 不用，Node类实例化时默认Next指针为null
                tail.Next = n;
                tail = n;
            }
        }
        public void printInfo(Action action)
        {
            Node<T> p = head;
            while (p != null)
            {
                Console.Write(p.Data + " ");
                p = p.Next;
            }
            action();
        }

    }




    class Program
    {


        static void Main(string[] args)
        {

            GenericList<int> list = new GenericList<int>();
            for(int i = 7; i < 15; i++)
            {
                list.Add(i * 3 - 6);
            }
            list.printInfo(()=> {
                int mmax = list.Head.Data;
                int mmin = list.Head.Data;
                int total = 0;
                Node<int> p = list.Head;
                while(p != null)
                {
                    mmax = (p.Data > mmax) ? p.Data : mmax;
                    mmin = (p.Data < mmin) ? p.Data : mmin;
                    total += p.Data;
                    p = p.Next;
                }
                Console.WriteLine("\n最大值为{0}，最小值为{1}，总和为{2}", mmax, mmin, total);
            });
        }
    }
}
